
#include <BFont>
#include <BPainter>
#include <BStyle>
#include <BMouseEvent>

#include <BCheckBox>
#include "member_BButton.h"

using namespace BWE;

BCheckBox::BCheckBox(BWidget* parent) : BButton(parent)
{
	this->setAlign(Align_LeftCenter);
	this->setForm(Form_IconText);
	this->setCheckable(true);
}
BCheckBox::BCheckBox(const BString& text, BWidget* parent) : BButton(text, parent)
{
	this->setAlign(Align_LeftCenter);
	this->setForm(Form_IconText);
	this->setCheckable(true);
}
BCheckBox::BCheckBox(Form form, BWidget* parent) : BButton(form, parent)
{
	this->setAlign(Align_LeftCenter);
	this->setForm(Form_IconText);
	this->setCheckable(true);
}
BCheckBox::BCheckBox(Form form, const BString& text, BWidget* parent) : BButton(form, text, parent)
{
	this->setAlign(Align_LeftCenter);
	this->setCheckable(true);
}
BCheckBox::~BCheckBox()
{

}

void BCheckBox::paintEvent(const BEvent& event)
{
	BButton::paintEvent(event);
	if (event.accepted())
		return;
	if (button_member(this)->graph <= Graph_Round_Fill && button_member(this)->checkable)
	{
		BPainter painter(this);

		State state = painter.state();
		if (button_member(this)->checked)
			state = State_Checked;
		if (this->hovered())
		{
			if (button_member(this)->pressed)
				state |= State_Pressed;
			else
				state |= State_Hovered;
		}

		float wireSize = 1.0f;
		const BValue& wireSizeValue = painter.value(Value_Wire_Size);
		if (wireSizeValue.valid())
			wireSize = wireSizeValue;

		painter.setLineSmooth(true);
		painter.setLineWidth(wireSize);
		if (!painter.setColor(button_member(this)->color))
		{
			painter.setColor(Color_Text, state);
		}

		BRectf iconRect = button_member(this)->alignment.ia_iconRect;
		painter.drawRect(iconRect % 86);
		if (state & State_Checked)
		{
			iconRect %= 56;
			BPointf p0 = iconRect.leftCenter();
			BPointf p1 = iconRect.bottomCenter();
			BPointf p2 = iconRect.rightTop();
			painter.drawLine(p0, p1);
			painter.drawLine(p1, p2);
		}
	}
}
